const fs = require('fs');
const path = require('path');
const { Sequelize, DataTypes } = require('sequelize'); // 添加 DataTypes 导入
const sequelize = require('../config/database');

const models = {};

fs.readdirSync(__dirname)
  .filter(file => file !== 'index.js' && file.endsWith('.js'))
  .forEach(file => {
    // 修复参数传递
    const model = require(path.join(__dirname, file))(sequelize, DataTypes);
    models[model.name] = model;
  });

// 关联关系处理保持不变
Object.values(models)
  .filter(model => typeof model.associate === 'function')
  .forEach(model => model.associate(models));

module.exports = { ...models, sequelize };